<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Warehouse_in extends AdminController
{
    public $_validation = null;

    function __construct()
    {
        parent::__construct();

        $this->load->model('MShop');
        $this->load->model('MShippingIn');
        $this->load->model('MAdmin');
        $allShops = $this->MShop->getAllShopsTrue();
        $allPurchaser = $this->MAdmin->getAllPurchaser();
        $this->_validation = Array(
            array('field' => 'purchase_person', 'label' => '采购人', 'rules' => 'required')
        , array('field' => 'product_name', 'label' => '商品名称', 'rules' => 'required')
        , array('field' => 'shop_id', 'label' => '所属账户', 'rules' => 'integer|required')
//        , array('field' => 'group_id', 'label' => '所属销售组', 'rules' => 'integer|required')
        , array('field' => 'in_mark', 'label' => '入库类型', 'rules' => 'integer|required')
        , array('field' => 'in_status', 'label' => '入库状态', 'rules' => 'integer|required')
        , array('field' => 'batch', 'label' => '商品批次', 'rules' => 'trim|required|max_length[255]')
        , array('field' => 'warehouse', 'label' => '存储仓库', 'rules' => 'integer|required')
        , array('field' => 'warehouse_area', 'label' => '仓库区', 'rules' => 'integer|required')
        , array('field' => 'purchase_time', 'label' => '采购时间', 'rules' => 'required')
        , array('field' => 'product_price', 'label' => '商品单价', 'rules' => 'required')
        , array('field' => 'product_quantity', 'label' => '商品数量', 'rules' => 'required')
        , array('field' => 'left_quantity', 'label' => '剩余数量', 'rules' => 'required')
        , array('field' => 'product_amount', 'label' => '商品总价', 'rules' => 'required')
        , array('field' => 'remark', 'label' => '备注', 'rules' => '')
        , array('field' => 'update_time', 'label' => '更新时间', 'rules' => '')
        );
        $this->assign('allShops', $allShops);
        $this->assign('allPurchaser', $allPurchaser);
    }

    public function index()
    {
        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $this->formfilter->addFilter('product_name', 'like');
        $this->formfilter->addFilter('purchaser_id', 'where');
        $this->formfilter->addFilter('purchase_person', 'like');
        $this->formfilter->addFilter('batch', 'like');
        $this->formfilter->addFilter('shop_id', 'where');
        $this->formfilter->addFilter('in_mark', 'where');
        $this->formfilter->addFilter('warehouse', 'where');
        $this->formfilter->addFilter('in_status', 'where');

        if ($begintime = filterValue('in_create_time_begin')) {
            $this->formfilter->addFilter('in_create_time_begin', 'where', array('purchase_time >= ', strtotime($begintime . ' 00:00:00')));
        }
        if ($endtime = filterValue('in_create_time_end')) {
            $this->formfilter->addFilter('in_create_time_end', 'where', array('purchase_time <= ', strtotime($endtime . ' 23:59:59')));
        }

        $number_perpage = filterValue('in_number_perpage');
        $order_type = filterValue('in_order_type');

        $total_number = $this->MShippingIn->getCount();
        $limit = $this->pagination($total_number,'',$number_perpage);
        $list = $this->MShippingIn->getList($limit,$order_type);
        $this->assign('list', $list);
        $this->layout();
    }

    public function add()
    {
        $this->load->model('MShippingIn');
        $obj = $this->MShippingIn->createVo();
        $obj->operator_id = $this->_user->id;
        $this->load->library('form_validation');

        $shop = $this->MShop->getOne(['id' => $this->input->post('shop_id')]);

        $this->form_validation->set_rules($this->_validation);
        if ($this->form_validation->run() == TRUE) {
            $this->load->helper('populate');
            $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
            $newObj->purchase_time = strtotime($this->input->post('purchase_time'));
            $newObj->group_id = $shop->group_id;

            $result = $this->MShippingIn->add($newObj);
            if ($result) {

                //写入操作记录
                $content = ' 新增入库记录: ';
                foreach ($newObj as $key => $value) {
                    if ($key == 'product_price') {
                        $newObj->$key = (float)$newObj->$key;
                    }

                    //给数字赋值
                    if ($key == 'shop_id') {
                        $this->load->model('MShop');
                        $allShops = $this->MShop->getAllShopsTrue();
                        $newObj->shop_id = $allShops[$newObj->shop_id];
                    }

                    if ($key == 'in_status') {
                        $newObj->in_status = MShippingIn::getState($newObj->shop_id);
                    }

                    if ($key == 'warehouse') {
                        $newObj->warehouse = IOSS_StaticConfig::getShippingWarehouse($newObj->warehouse);
                    }
                    if ($key == 'warehouse_area') {
                        $newObj->warehouse_area = IOSS_StaticConfig::getShippingBlock($newObj->warehouse_area);
                    }

                    if ($key == 'in_mark') {
                        $newObj->in_mark = IOSS_StaticConfig::getShippingInType($newObj->in_mark);
                    }

                    if ($key == 'purchase_time') {
                        $newObj->purchase_time = date('Y-m-d', $newObj->purchase_time);
                    }

                    if ($key !== 'update_time' && $key !== 'create_time') {
//                        $content .= ' 新增入库记录：' . IOSS_StaticConfig::translateLogField($key) . ' 为 ' . $obj->$key . $newObj->$key . ';';
                        $content .=  $key . ' 为 ' .  $newObj->$key . ';';
                    }
                }
                if ($content) {
                    $this->shippingLog($this->_user->id, $result, $this->_thisModule . $this->_thisController . '/' . $this->_thisMethod, $content,1);
                }
                successAndRedirect('添加入库成功');
            }else {
                errorAndRedirect(l('data_fail'));
            }
        }
        $this->assign('obj', $obj);
        $this->layout();
    }

    function edit($id)
    {
        if (!is_numeric($id)) {
            show_error(l('id_not_null'));
        }
        $obj = $this->MShippingIn->getOne(Array('id' => $id));
        if (!$obj) {
            errorAndRedirect('未找到定义！');
        }

        $shop = $this->MShop->getOne(['id' => $this->input->post('shop_id')]);
        $this->editObj = $obj;
        $obj->purchase_time = date('Y-m-d',$obj->purchase_time);
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->_validation);

        if ($this->form_validation->run() == TRUE) {
            $this->load->helper('populate');
            $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
            $newObj->purchase_time = strtotime($this->input->post('purchase_time'));
            $newObj->group_id = $shop->group_id;
            if ($this->MShippingIn->update($newObj)) {
                //写入操作记录
                $content = ' 更新入库记录: ';
                foreach ($newObj as $key => $value) {
                    if ($key == 'product_price') {
                        $newObj->$key = (float)$newObj->$key;
                        $obj->$key = (float)$obj->$key;
                    }

                    //给数字赋值
                    if ($key == 'shop_id') {
                        $this->load->model('MShop');
                        $allShops = $this->MShop->getAllShopsTrue();
                        $newObj->shop_id = $allShops[$newObj->shop_id];
                        $obj->shop_id = $allShops[$obj->shop_id];
                    }

                    if ($key == 'in_status') {
                        $newObj->in_status = MShippingIn::getState($newObj->in_status);
                        $obj->in_status = MShippingIn::getState($obj->in_status);
                    }

                    if ($key == 'warehouse') {
                        $newObj->warehouse = IOSS_StaticConfig::getShippingWarehouse($newObj->warehouse);
                        $obj->warehouse = IOSS_StaticConfig::getShippingWarehouse($obj->warehouse);
                    }
                    if ($key == 'warehouse_area') {
                        $newObj->warehouse_area = IOSS_StaticConfig::getShippingBlock($newObj->warehouse_area);
                        $obj->warehouse_area = IOSS_StaticConfig::getShippingBlock($obj->warehouse_area);
                    }

                    if ($key == 'in_mark') {
                        $newObj->in_mark = IOSS_StaticConfig::getShippingInType($newObj->in_mark);
                        $obj->in_mark = IOSS_StaticConfig::getShippingInType($obj->in_mark);
                    }

                    if ($key == 'purchase_time') {
                        $newObj->purchase_time = date('Y-m-d', $newObj->purchase_time);
                    }
                    if ($newObj->$key !== $obj->$key && $key !== 'update_time' && $key !== 'create_time'){

//                        $content .= ' '.IOSS_StaticConfig::translateLogField($key).' 由 '.$obj->$key.' 改为 '.$newObj->$key.';';
                        $content .= $key.' 由 '.$obj->$key.' 改为 '.$newObj->$key.';';
                    }
                }
                if ($content) {
                    $this->shippingLog($this->_user->id, $id, $this->_thisModule . $this->_thisController . '/' . $this->_thisMethod, $content,1);
                }
                successAndRedirect(l('edit_success'));
            } else {
                $this->db->trans_complete();
                errorAndRedirect(l('data_fail'));
            }
        }
        $this->assign('obj', $obj);
        $this->layout();
    }

    function view($id)
    {
        if (!is_numeric($id)) {
            show_error(l('id_not_null'));
        }
        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $obj = $this->MShippingIn->getOne(Array('id' => $id));
        if (!$obj) {
            errorAndRedirect('未找到定义！');
        }

        $this->load->model('MShippingLog');
        $logs = $this->MShippingLog->getAllInLog($id);

        $this->assign('obj', $obj);
        $this->assign('logs', $logs);
        $this->layout('view.tpl');
    }

    function delete($id, $update_time)
    {
        if (!is_numeric($id) || !is_numeric($update_time)) {
            show_error(l('id_not_null'));
        }

        $newObj = $this->MShippingIn->getById($id);

        //写入操作记录
        $content = ' 删除入库记录: ';
        foreach ($newObj as $key => $value) {
            if ($key == 'product_price') {
                $newObj->$key = (float)$newObj->$key;
            }

            //给数字赋值
            if ($key == 'shop_id') {
                $this->load->model('MShop');
                $allShops = $this->MShop->getAllShopsTrue();
                $newObj->shop_id = $allShops[$newObj->shop_id];
            }

            if ($key == 'in_status') {
                $newObj->in_status = MShippingIn::getState($newObj->shop_id);
            }

            if ($key == 'warehouse') {
                $newObj->warehouse = IOSS_StaticConfig::getShippingWarehouse($newObj->warehouse);
            }
            if ($key == 'warehouse_area') {
                $newObj->warehouse_area = IOSS_StaticConfig::getShippingBlock($newObj->warehouse_area);
            }

            if ($key == 'in_mark') {
                $newObj->in_mark = IOSS_StaticConfig::getShippingInType($newObj->in_mark);
            }

            if ($key == 'purchase_time') {
                $newObj->purchase_time = date('Y-m-d', $newObj->purchase_time);
            }

            if ($key !== 'update_time' && $key !== 'create_time') {
//                        $content .= ' 新增入库记录：' . IOSS_StaticConfig::translateLogField($key) . ' 为 ' . $obj->$key . $newObj->$key . ';';
                $content .=  $key . ' 为 ' .  $newObj->$key . ';';
            }
        }

        if (!$this->MShippingIn->delete($id, $update_time)) {

            errorAndRedirect(l('data_fail'));
        }else{
            if ($content) {
                $this->shippingLog($this->_user->id, $id, $this->_thisModule . $this->_thisController . '/' . $this->_thisMethod, $content,1);
            }
            successAndRedirect(l('delete_success'));
        }
    }
}
